<?php

//Created by Joanna Tan 

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class NewArrivals extends CI_Controller {

    /*
|--------------------------------------------------------------------------
| resultList (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| For Basic Search.
| This function will be use to check the search filter type, search 
| as a phrase option and returns the parsed Search XML results as 
| JSON and call the function to wrap the response in the
| requested callback. 
|
*/      
  

 

/*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];
        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }

/*
|--------------------------------------------------------------------------
| retrieveList (@author : Joanna Tan)
|--------------------------------------------------------------------------
|
| This function will returns the parsed Result XML results as 
| JSON and call the function to wrap the response in the
| requested callback. 
|
| This function will call sortXML function to sort the result by 
| default to year or depending on the user selection.
| 
*/  
	 public function retrieveList($condi) {
        $this->load->model('result_model');
        //part1 = setnumber, parttwo = start_record_number, part3 = sorttype, part4 = count
        list($part1, $part2, $part3, $part4) = explode("U5R", $condi);

        // $sorterBaseUrl = '';
        // $sorter2Url = '';
        // if ($part2 == '1') {
            // if ($part3 == 'na') {
            //    do nothing
            // } else {
                // $sorterBaseUrl = $this->config->item('sortBaseUrl');
                // if ($part3 == 'School') {
                    // $sorter2Url = $this->config->item('sortBySchool3');
                // } else if ($part3 == 'Year') {
                    // $sorter2Url = $this->config->item('sortByYear3');
                // } else if ($part3 == 'Title') {
                    // $sorter2Url = $this->config->item('sortByTitle2');
                // $sortXml = $sorterBaseUrl . $part1 . $sorter2Url;
                // $this->result_model->sortXml($sortXml);
            // }
        // }

        $newnum = 0;
        $pageentriesRange = '';
        for ($i = 0; $i < $part4; $i++) {
            if (empty($pageentriesRange)) {
                $newnum = $part2;
                $pageentriesRange = $newnum;
            } else {
                $newnum = $newnum + 1;
                $pageentriesRange = $pageentriesRange . "," . $newnum;
            }
        }

        $retrieve1Url = $this->config->item('retrieve1Url');
        $retrieve2Url = $this->config->item('retrieve2Url');
        $retrieve3Url = $this->config->item('retrieve3Url');

        $xmlTwo = $retrieve1Url . $part1 . $retrieve2Url . $pageentriesRange . $retrieve3Url;
        $output = $this->result_model->praseSecondXml($xmlTwo, $part2);
        $bookdata = array();
        $bookdata['output'] = $output;
        $this->retJsonP($output);
    }
	public function loadNewAList() {
        $this->load->model('result_model');
        $quotes = "%22";
        $xmlOne = '';
        $searchUrl = $this->config->item('newarrival_openshelf');
        $filtername = '';
      //  $optionChecked = '';
		$keyword ='';
		// list($filtertype) = explode("U5R", $conditionBasic);
       
            // $filtername = 'nwb';


        // if ($option == '1') {
            // $optionChecked = 'on';
        // } else if ($option == '0') {
            // $optionChecked = 'off';
        // }

        //replace word with encodings
     // $keywordcheckSlash = $this->checkWord($keyword);

       // if ($optionChecked == 'on') {
           // $xmlOne = $searchUrl . $filtername . "=" . $quotes . $keywordcheckSlash . $quotes;
       // } else if ($optionChecked == 'off') {
            $xmlOne = $searchUrl . $filtername ;
       // }
        //replace encoding back to symbols
         // $keywordpro = $this->replaceWord($keyword);
        $output = $this->result_model->praseFirstXml($xmlOne,$keyword);
        $this->retJsonP($output);
    }
	
	public function newAList($conditionBasic) {
        $this->load->model('result_model');
        $quotes = "%22";
        $xmlOne = '';
        $searchUrl = '';
        $filtername = '';
      //  $optionChecked = '';
		$keyword ='';
        list($filtertype) = explode("U5R", $conditionBasic);

        if ($filtertype == 'newshelfbooks') {
            $searchUrl = $this->config->item('newarrival_openshelf');
        } else if ($filtertype == 'nebooks') {
           $searchUrl = $this->config->item('newarrival_eBooks');
        } else if ($filtertype == 'AVitems') {
           $searchUrl = $this->config->item('newarrival_av');
        } else if ($filtertype == 'novels') {
            $searchUrl = $this->config->item('newarrival_Fiction');
        }else if ($filtertype == 'redspot') {
           $searchUrl = $this->config->item('newarrival_redspot');
        }

            $xmlOne = $searchUrl;
        $output = $this->result_model->praseFirstXml($xmlOne,$keyword);
        $this->retJsonP($output);
    }
	
	
     public function retrieveSortList($condi) {
        $this->load->model('result_model');
        //part1 = setnumber, parttwo = start_record_number, part3 = sorttype, part4 = count
        list($part1, $part2, $part3, $part4) = explode("U5R", $condi);

        $sorterBaseUrl = '';
        $sorter2Url = '';
        if ($part2 == '1') {
            if ($part3 == 'na') {
                //do nothing
            } else {
                $sorterBaseUrl = $this->config->item('sortBaseUrl2');
                if ($part3 == 'Year') {
                    $sorter2Url = $this->config->item('sortByNYear');
                } else if ($part3 == 'Title') {
                    $sorter2Url = $this->config->item('sortByNTitle');
                } else if ($part3 == 'Format') {
                    $sorter2Url = $this->config->item('sortByNFormat');
                } else if ($part3 == 'Author') {
                    $sorter2Url = $this->config->item('sortByNAuthor');
                } else if ($part3 == 'Callnum') {
                    $sorter2Url = $this->config->item('sortByNCallNum');
                }
                $sortXml = $sorterBaseUrl . $part1 . $sorter2Url;
                $this->result_model->sortXml($sortXml);
            }
        }

        $newnum = 0;
        $pageentriesRange = '';
        for ($i = 0; $i < $part4; $i++) {
            if (empty($pageentriesRange)) {
                $newnum = $part2;
                $pageentriesRange = $newnum;
            } else {
                $newnum = $newnum + 1;
                $pageentriesRange = $pageentriesRange . "," . $newnum;
            }
        }

        $retrieve1Url = $this->config->item('retrieve1Url');
        $retrieve2Url = $this->config->item('retrieve2Url');
        $retrieve3Url = $this->config->item('retrieve3Url');

        $xmlTwo = $retrieve1Url . $part1 . $retrieve2Url . $pageentriesRange . $retrieve3Url;
        $output = $this->result_model->praseSecondXml($xmlTwo, $part2);
        $bookdata = array();
        $bookdata['output'] = $output;
        $this->retJsonP($output);
    }
	// public function resultList($conditionBasic) {
        // $this->load->model('result_model');
        // $quotes = "%22";
        // $xmlOne = '';
        // $searchUrl = $this->config->item('newarrival_xml');
        // $filtername = '';
      // //  $optionChecked = '';
		// $keyword ='';
        // list($filtertype) = explode("U5R", $conditionBasic);

        // if ($filtertype == 'Year') {
            // $filtername = 'yr';
        // } else if ($filtertype == 'Author') {
            // $filtername = 'aut';
        // } else if ($filtertype == 'Title') {
            // $filtername = 'tle';
        // } else if ($filtertype == 'Format') {
            // $filtername = 'fm';
        // }else if ($filtertype == 'Callnum') {
            // $filtername = 'cn';
        // }

        // // if ($option == '1') {
            // // $optionChecked = 'on';
        // // } else if ($option == '0') {
            // // $optionChecked = 'off';
        // // }

        // //replace word with encodings
     // // $keywordcheckSlash = $this->checkWord($keyword);

       // // if ($optionChecked == 'on') {
           // // $xmlOne = $searchUrl . $filtername . "=" . $quotes . $keywordcheckSlash . $quotes;
       // // } else if ($optionChecked == 'off') {
            // $xmlOne = $searchUrl . $filtername ;
       // // }
        // //replace encoding back to symbols
         // // $keywordpro = $this->replaceWord($keyword);
        // $output = $this->result_model->praseFirstXml($xmlOne,$keyword);
        // $this->retJsonP($output);
    // }
	
}

?>
